<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
class GoodsController extends Controller
{
    //添加商品
    public function getAdd()
    {

    	$data = DB::table('cate') 
            -> select('*',DB::raw("concat(path,',',id) as sortpath"))
            -> orderBy('sortpath')
            -> get();

        foreach ($data as $k => $v) {
            $num = substr_count($v -> path, ',');

            $data[$k] -> name = str_repeat('|----', $num).$v -> name;
            
        }

    	return view('admin.goods.add',['data' => $data]);
       
    }

    //insert
    public function postInsert(Request $request)
    {
        $data = $request -> except('_token');
        // $res = DB::table('goods') -> insert($data);
        // dd($data);
        if($request -> hasFile('img_head'))
        {
            if($request -> file('img_head') -> isValid())
            {
                //移动文件
                $suffix = $request -> file('img_head') -> getClientOriginalExtension();
                //随机一个文件名
                $filename = time().mt_rand(100000,9999999);

                //移动文件
                $request -> file('img_head') -> move('./uploads', $filename.'.'.$suffix);
                $data['img_head'] = $filename.'.'.$suffix;

                $data['status'] = 0;
                $data['time'] = date('Y-m-d H:i:s');
                $res = DB::table('goods') -> insert($data);
                if($res)
                {
                    return redirect('/admin/goods/index') -> with(['info' => '添加成功']);
                }else
                {
                    return back() -> with(['info' => '添加失败']);
                }
            }

        }else
        {
            return back() -> with(['info' => '请上传顶部图片']);
        }
        
        

    }

    //商品列表
    public function getIndex(Request $request)
    {
        $data = DB::table('goods') 
            -> select('goods.*','cate.name as cname' )
            -> Leftjoin('cate', 'goods.cate_id', '=', 'cate.id')
            -> where('goods.title', 'like', '%'.$request -> input('keywords').'%')
            -> orderBy('id', 'desc')
            -> paginate($request -> input('num', 10));
        return view('admin.goods.index', ['data' => $data, 'request' => $request -> all()]);

    }
    public function getEdit($id)
    {
       $allData = DB::table('cate') 
            -> select('*',DB::raw("concat(path,',',id) as sortpath"))
            -> orderBy('sortpath')
            -> get();

        foreach ($allData as $k => $v) {
            $num = substr_count($v -> path, ',');

            $allData[$k] -> name = str_repeat('|----', $num).$v -> name;
            
        }

        $data = DB::table('goods') -> where('id', $id) -> first();

        return view('admin.goods.edit',['data' => $data, 'allData' => $allData]);

        // dd($allData, $data);
    }
    //update
    public function postUpdate(Request $request)
    {
        $data = $request -> except('_token');


        
        //处理图片
        if($request -> hasFile('img_head'))
        {
            if($request -> file('img_head') -> isValid())
            {

                //查询老图片

                $img = DB::table('goods') -> where('id', $data['id']) -> first() -> img_head;
                //老图片路径
                $oldImg = './uploads/'.$img;
                //移动文件
                $suffix = $request -> file('img_head') -> getClientOriginalExtension();
                //随机一个文件名
                $filename = time().mt_rand(100000,9999999);

                //移动文件
                $request -> file('img_head') -> move('./uploads', $filename.'.'.$suffix);
                // $data['img_head'] = $filename.'.'.$suffix;

                if(file_exists($oldImg))
                {
                    unlink($oldImg);
                }

                $data['img_head'] = $filename.'.'.$suffix;

            }

        }
        //执行sql
        $res = DB::table('goods') -> where('id', $data['id']) -> update($data);
         if($res)
        {
            return redirect('/admin/goods/index') -> with(['info' => '修改成功']);
        }else
        {
            return back() -> with(['info' => '修改失败']);
        }
    }
    public function postAjaxupdate(Request $request)
   {
        $id = $request -> input('id');

        
        $data = DB::table('goods') -> select('status') -> where('id',$id) -> first();
        //判断
        if($data -> status == 0)
        {
            $res = DB::table('goods') -> where('id', $id) -> update(['status' => 1]);
            
            
            if($res)
            {
                return response() -> json(1);
            }else
            {
                return response() -> json(5);
            }
        }else 
        {
            $res = DB::table('goods') ->where('id', $id) -> update(['status' => 0]);
            
            if($res)
            {
                return response() -> json(0);
            }else
            {
                return response() -> json(5);
            }
        }
   }
   //ajaxName
   public function postAjaxName(Request $request)
   {
        
        // dd($request -> all());
        $res = DB::table('goods') -> where('title', $request -> input('title')) -> first();
        if($res)
        {
            return response() -> json(5);
        }else
        {
            $r = DB::table('goods') -> where('id', $request -> id) -> update(['title' => $request -> title]);
            if($r)
            {

                return response() -> json(0);
            }else
            {
                return response() -> json(1);
            }
        }
   }
   //删除
   public function getDelete($id)
   {
        $img = DB::table('goods') -> where('id', $id) -> first() -> img_head;
        $oldImg = './uploads/'.$img;

        // dd($oldImg);
        $res = DB::table('goods') -> delete($id);
        if($res)
        {
            if(file_exists($oldImg))
            {
                unlink($oldImg);
            }
            return redirect('/admin/goods/index') -> with(['info' => '删除成功']);
        }else
        {
            return back() -> with(['info' => '删除失败']);
        }
   }
   public function getContent($id)
   {
        $allData = DB::table('cate') 
            -> select('*',DB::raw("concat(path,',',id) as sortpath"))
            -> orderBy('sortpath')
            -> get();

        foreach ($allData as $k => $v) {
            $num = substr_count($v -> path, ',');

            $allData[$k] -> name = str_repeat('|----', $num).$v -> name;
            
        }

        $data = DB::table('goods') -> where('id', $id) -> first();
        return view('admin.goods.content',['data' => $data, 'allData' => $allData]);

   }

   public function postLook(Request $request)
   {
         $data = $request -> except('_token');


        
       
        $res = DB::table('goods') -> where('id', $data['id']) -> update($data);
        
        return redirect('/admin/goods/index');
      
   }
}